package com.orangelife.mobile.complaints.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.curry.android.adapter.FatherAdapter;
import com.orangelife.mobile.complaints.activity.SelectHouseActivity;
import com.orangelife.mobile.zoning.activity.SelectZoningActicity;
import com.orangelife.mobile.zoning.adapter.SelectZoningAdapter;

import java.util.List;
import java.util.Map;

public class MyHousesAdapter<T>  extends FatherAdapter<T> {

	private List<Map<String, Object>> list;
	private Context context;
	private int type = SelectZoningActicity.WHAT_PROVINCE;

	public MyHousesAdapter(Context context, List<Map<String, Object>> list2, int type) {
		this.list = list2;
		this.context = context;
		this.type = type;
	}

	public void setType(int type) {
		this.type = type;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_zoning, null);
			viewHolder.tvZoning = (TextView) convertView
					.findViewById(R.id.tv_zoning);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		String value = getValue(position);
		viewHolder.tvZoning.setText(value);
		return convertView;
	}

	private String getValue(int position) {
		String name = "未知";
		String key = "";
		switch (type) {
			case SelectHouseActivity.WHAT_APARTMENT:
				key = "apartmentNumb";
				break;
			case SelectHouseActivity.WHAT_BUILDING:
				key = "buildingName";
				break;
			case SelectHouseActivity.WHAT_CITY:
				key = "city";
				break;
			case SelectHouseActivity.WHAT_AREA:
				key = "comm";
				break;
			default:
				return name;
		}
		name = String.valueOf(list.get(position).get(key));
		return name;
	}

	class ViewHolder {
		TextView tvZoning;
	}

	@Override
	@SuppressWarnings("unchecked")
	public BaseAdapter setList(List<T> list) {
		this.list = (List<Map<String, Object>>) list;
		return MyHousesAdapter.this;
	}
}
